package com.webemail.views.controller;

import com.webemail.sys.model.SysEmailAccount;
import com.webemail.sys.model.SysFolder;
import com.webemail.sys.model.SysLabel;
import com.webemail.sys.model.SysMessageDrafts;
import com.webemail.sys.service.EmailService;
import com.webemail.sys.service.FolderService;
import com.webemail.sys.service.LabelService;
import com.webemail.sys.service.MessageDraftsService;
import com.webemail.sys.util.CommonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LaiHQ
 */
@Controller
@RequestMapping("/drafts")
public class EmailDraftsControllerView {
	@Resource
	private EmailService emailService;
	@Resource
	private FolderService folderService;
	@Resource
	private LabelService labelService;
	@Resource
	private MessageDraftsService messageDraftsService;
	private int getUserId() {
		return 1;
	}

	@RequestMapping("/all")
	public String all(HttpServletRequest request, Model model) {
		long userId = this.getUserId();
		List<SysLabel> labelList = new ArrayList<SysLabel>();
		List<SysFolder> folderList = new ArrayList<SysFolder>();
		try {
			labelList = labelService.list(userId);
			folderList = folderService.list(userId);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("msg", e.getMessage());
			return "mail/error";
		}
		model.addAttribute("labelList", labelList);
		model.addAttribute("folderList", folderList);
		return "mail/message_drafts";
	}

	@RequestMapping("/detail")
	public String emailDetail(HttpServletRequest request, Model model) {
		String id = null;
		try {
			id = CommonUtils.getString(request.getParameter("id"));
			int idTemp = Integer.parseInt(id);
			SysMessageDrafts result = messageDraftsService.get(idTemp, this.getUserId());
			model.addAttribute("message", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<SysEmailAccount> list = emailService.list(this.getUserId());
		model.addAttribute("mailList", list);
		return "mail/email_deafts";
	}
}
